package com.yanghui.lingyue.utils.databaseUtils

import com.yanghui.lingyue.bot.Bot
import com.yanghui.lingyue.data.Operation
import java.sql.PreparedStatement
import java.sql.SQLException

object OperationDatabaseUtils {

//    @Throws(SQLException::class)
//    fun insertOperation(operation: Operation): Long {
//        val sql = "INSERT INTO Operation (id, userID, operationCode, operationTime, groupID) " +
//                "VALUES (?, ?, ?, ?, ?)"
//        val statement: PreparedStatement = getStatement(sql)
//        val num: Long = getOperationNum()
//        statement.setLong(1, num + 1)
//        statement.setLong(2, operation.userID)
//        statement.setInt(3, operation.instructionCode)
//        statement.setTimestamp(4, Timestamp(Date().time))
//        statement.setLong(5, operation.userID)
//        statement.executeUpdate()
//        statement.close()
//        return num + 1
//    }

    @Throws(SQLException::class)
    fun insertOperation(operation: Operation) {
        Bot.sikaCore.databaseInsert(operation)
    }

    @Throws(SQLException::class)
    fun getOperationNum(): Long {
        val sql = "SELECT Count(*) FROM Operation"
        val statement: PreparedStatement = Bot.sikaCore.getStatement(sql)
        val resultSet = statement.executeQuery()
        resultSet.next()
        val num = resultSet.getInt(1)
        statement.close()
        return num.toLong()
    }

    @Throws(SQLException::class)
    fun operationAddTarget(operationId: Long, target: Long) {
        val sql = "UPDATE Operation SET target = ? WHERE id = ?"
        val statement = Bot.sikaCore.getStatement(sql)
        statement.setLong(1, target)
        statement.setLong(2, operationId)
        statement.executeUpdate()
        statement.close()
    }

    fun operationAddTarget(operation: Operation) {
        Bot.sikaCore.databaseUpdate(operation)
    }

}